package com.zyj.myapplication.Activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.zyj.myapplication.Adapter.CollectListAdapter;
import com.zyj.myapplication.Javabean.UserCollect;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserCollectActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ImageView collectlist_return;
    private int user_id;
    private String user_photo;
    private String user_name;
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usercollect);
        initViews();//初始化
        initEvents();//初始化事件
        showData();
    }


    private void initViews() {
        context = UserCollectActivity.this;
        recyclerView = (RecyclerView) findViewById(R.id.user_collect_rv);
        collectlist_return = (ImageView) findViewById(R.id.collectlist_return);
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id", 0);
    }

    private void initEvents() {
        collectlist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void showData() {
        UserModel userModel = new UserModel();
        Call<List<UserCollect>> call = userModel.getcollect(user_id);
        call.enqueue(new Callback<List<UserCollect>>() {
            @Override
            public void onResponse(Call<List<UserCollect>> call, Response<List<UserCollect>> response) {
                List<UserCollect> mDataList = response.body();
                LinearLayoutManager manager = new LinearLayoutManager(context);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(new CollectListAdapter(UserCollectActivity.this, mDataList,user_id));
            }

            @Override
            public void onFailure(Call<List<UserCollect>> call, Throwable t) {
                Toast.makeText(context, "获取列表失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
